<?php 
/*
Plugin Name: DE Filter
Plugin URI: www.craftsmancoding.com
Description: THis Plugin will list some posts related to the current category of current page
Author: Daniel Edano
*/ 
add_filter('the_content', function de_filter($content) {

	//instead of the_id use get_the_id, the_id will print the value
	$id = get_the_id();
	//if list of posts do nothing
	if( !is_singular('post') ) {
		return $content;
	};

	$terms = get_the_terms( $id, 'category' );
	$cats = array();

	foreach ($terms as $term) {
		$cats[] = $term->cat_ID;
	}

	$loop = new WP_Query(
		array(
			'posts_per_page'=>3,
			'category__in'=> $cats,
			'orderby'=> 'rand',
			'post__not_in'=>array($id)
		)
	);

	if( $loop->have_posts() ) {
		$content .= '<h2>You might also like...</h2>';
		$content .= '<ul class="related-cate-posts">';

		while( $loop->have_posts() ) {
			$loop->the_post();
			$content .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
		}
		$content .='</ul>';
		wp_reset_query();
	}

	return $content;

});



